﻿@{
    Layout = "~/_SiteLayout.cshtml";
    
    Business.Platform.Organize borganize = new Business.Platform.Organize();
    Business.Platform.Users busers = new Business.Platform.Users();

    string id = Request.QueryString["id"];

    string name = string.Empty;
    string account = string.Empty;
    string status = string.Empty;
    string note = string.Empty;

    Validation.RequireFields("Name", "Account", "Status");
    Guid parentID;
  
    if(IsPost && Validation.IsValid() && id.IsGuid(out parentID))
    {
        name = Request.Form["Name"];
        account = Request.Form["Account"];
        status = Request.Form["Status"];
        note = Request.Form["Note"];

        Guid userID = Guid.NewGuid();
        string userXML=string.Empty;
        using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
        {
            //添加人员
            Data.Model.Users user = new Data.Model.Users();
            user.Account = account.Trim();
            user.Name = name.Trim();
            user.Note = note.IsNullOrEmpty() ? null : note;
            user.Password = busers.GetUserEncryptionPassword(userID.ToString(), busers.GetInitPassword());
            user.Sort = 1;
            user.Status = status.IsInt() ? status.ToInt() : 0;
            user.ID = userID;
            busers.Add(user);

            //添加关系
            Data.Model.UsersRelation userRelation = new Data.Model.UsersRelation();
            userRelation.IsMain = 1;
            userRelation.OrganizeID = parentID;
            userRelation.Sort = new Business.Platform.UsersRelation().GetMaxSort(parentID);
            userRelation.UserID = userID;
            new Business.Platform.UsersRelation().Add(userRelation);

            //更新父级[ChildsLength]字段
            borganize.UpdateChildsLength(parentID);
            
            //更新角色
            new Business.Platform.UsersRole().UpdateByUserID(userID);
            
            userXML=user.Serialize();
            
            scope.Complete();
        }

        Business.Platform.Log.Add("添加了人员", userXML, Business.Platform.Log.Types.组织机构);
        
        <script type="text/javascript">
            parent.frames[0].reLoad('@id');
            alert('添加成功!');
            window.location=window.location;
        </script>
        
        
    }
    

    string statusRadios = borganize.GetStatusRadio("Status", "0", "validate=\"radio\"");
}

<form action="" method="post">
    <br />
    <table cellpadding="0" cellspacing="1" border="0" width="95%" class="formtable">
        <tr>
            <th style="width:80px;">姓名：</th>
            <td><input type="text" id="Name" name="Name" class="mytext" value="@name" onchange="getPy(this.value);" validate="empty,min,max" max="50" style="width:160px;" /></td>
        </tr>
        <tr>
            <th style="width:80px;">帐号：</th>
            <td><input type="text" id="Account" name="Account" class="mytext" value="@account" validate="empty,max,ajax" validate_url="CheckAccount" max="20" style="width:160px;" /></td>
        </tr>
        <tr>
            <th style="width:80px;">状态：</th>
            <td>@Html.Raw(statusRadios)</td>
        </tr>
        <tr>
            <th style="width:80px;">备注：</th>
            <td><textarea id="Note" name="Note" class="mytext" style="width:90%; height:50px;" >@note</textarea></td>
        </tr>
        
    </table>
    <div style="width:95%; margin:10px auto 10px auto; text-align:center;">
        <input type="submit" class="mybutton" name="Save" value="保存" />
        <input type="button" class="mybutton" onclick="window.location='Body'+'@Request.Url.Query';" value="返回" />
    </div>
    </form>
    <script type="text/javascript">
        function getPy(v)
        {
            $.ajax({ url: 'GetPy', data: { 'name': v }, dataType: 'text', type: 'post', cache: false, success: function (txt)
            {
                $('#Account').val(txt);
            }
            });
        }
    </script>